package com.web.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.entity.LoginInfo;
import com.web.entity.PageResult;
import com.web.entity.User;
import com.web.mapper.UserMapper;
import com.web.service.UserService;
import com.web.utils.JwtUtils;
import io.jsonwebtoken.JwtBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public void updateRealNameAndIntroduce(User user) {

        user.setUpdateTime(String.valueOf(LocalDateTime.now()));
        this.userMapper.updateRealNameAndIntroduce(user);
    }

    @Override
    public void updatePhone(User user) {
        user.setUpdateTime(String.valueOf(LocalDateTime.now()));
        this.userMapper.updatePhone(user);
    }

    @Override
    public void updateEmail(User user) {
        user.setUpdateTime(String.valueOf(LocalDateTime.now()));
        this.userMapper.updateEmail(user);
    }

    @Override
    public Boolean updatePassword(User user,String oldPassword) {
        User byId = userMapper.findById(user.getUserId());
        if(!byId.getPassword().equals(oldPassword)){
            return false;
        }
        user.setUpdateTime(String.valueOf(LocalDateTime.now()));

        userMapper.updatePassword(user);
        return true;
    }

    @Override
    public LoginInfo login(User user) {
        User user1 = this.userMapper.getUsernameAndPassword(user);
        if(user1!=null){
            Map<String,Object> map = new HashMap<>();
            map.put("userId",user1.getUserId());
            map.put("username",user1.getUsername());
            map.put("realName",user1.getRealName());
            map.put("token",user1.getToken());
            String s = JwtUtils.generateJwt(map);
            LoginInfo loginInfo = new LoginInfo(user1.getUserId(),user1.getUsername(),user1.getRealName(),s);

            return loginInfo;
        }
        return null;
    }

    /*
        分页查询
         */
    @Override
    public PageResult<User> page(Integer page, Integer pageSize, User user){
        //设置分页参数
       PageHelper.startPage(page,pageSize);
       //执行查询
       List<User> userList = userMapper.list(user);
       //解析查询结果并封装
       Page<User> p = (Page<User>) userList;

       return new PageResult<User>(p.getTotal(),p.getResult());

   }

   /*
   id查询
    */
    @Override
    public User findById(Integer id){
        return userMapper.findById(id);
    }

    /*
    修改
     */
    @Override
    public void update(User user){
        userMapper.update(user);
    }

    @Override
    public void insert(User user){
        user.setCreateTime(String.valueOf(LocalDateTime.now()));
        user.setUpdateTime(String.valueOf(LocalDateTime.now()));
        userMapper.insert(user);
    }

    @Override
    public void delete(Integer id){
        userMapper.delete(id);

    }
}
